import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:winkai/entity/chat_msg.dart';
import 'package:winkai/helper/iap/iap_helper.dart';
import 'package:winkai/res/dimens.dart';
import 'package:winkai/ui/msg/controller/chat_controller.dart';
import 'package:winkai/ui/msg/widget/item/chat_msg_image_widget.dart';
import 'package:winkai/ui/msg/widget/item/chat_msg_text_widget.dart';
import 'package:winkai/ui/msg/widget/item/chat_msg_video_widget.dart';
import 'package:winkai/ui/msg/widget/item/chat_msg_vocie_widget.dart';
import 'package:winkai/ui/msg/widget/item/chat_msg_wait_widget.dart';
import 'package:winkai/utils/focus_utils.dart';

class ChatHistoryWidget extends StatefulWidget {
  const ChatHistoryWidget({super.key, required this.controller});

  final ChatController controller;

  @override
  State<ChatHistoryWidget> createState() => _ChatHistoryWidgetState();
}

class _ChatHistoryWidgetState extends State<ChatHistoryWidget> {
  late ChatController controller;

  @override
  void initState() {
    super.initState();
    controller = widget.controller;
  }

  @override
  Widget build(BuildContext context) {
    return Obx(() {
      var messageList = controller.messageList;
      IAPHelper().subscribeStatus.value;
      return Stack(
        alignment: Alignment.topCenter,
        children: [
          GestureDetector(
            onPanDown: (_) {
              FocusUtils.unFocus();
            },
            child: ListView.separated(
              physics: const BouncingScrollPhysics(),
              padding: EdgeInsets.symmetric(
                  horizontal: Dimens.mainHorizontalSpacing, vertical: 10.w),
              shrinkWrap: true,
              reverse: true,
              itemBuilder: (context, index) {
                var message = messageList[index];
                if (message.type == ChatMsg.typeVideo) {
                  return ChatMsgVideoWidget(message: message);
                } else if (message.type == ChatMsg.typeImage) {
                  return ChatMsgImageWidget(
                    message: message,
                    onAvatarClick: () {
                      controller.jumpUserDetails();
                    },
                  );
                } else if (message.type == ChatMsg.typeVoice) {
                  return ChatMsgVoiceWidget(message: message);
                } else if (message.type == ChatMsg.typeWaitAnswer) {
                  return ChatMsgWaitWidget(
                    message: message,
                    onRetryClick: () {
                      controller.showReloadMsgDialog(context, message);
                    },
                    onAvatarClick: () {
                      controller.jumpUserDetails();
                    },
                  );
                } else {
                  return ChatMsgTextWidget(
                    message: message,
                    onRetryClick: () {
                      controller.showReplyMsgDialog(context, message);
                    },
                    onAvatarClick: () {
                      controller.jumpUserDetails();
                    },
                  );
                }
              },
              separatorBuilder: (context, index) {
                return SizedBox(height: 16.w);
              },
              itemCount: messageList.length,
            ),
          ),
        ],
      );
    });
  }
}
